package com.expriment.polynomial;

import com.algorithm.core.FHOPEAlgorithm;

/**
 * 该类用于测试FHOPE是否支持复杂的多项式操作,如a * b + c > d这样的表达式是否能够成立。
 * @author Haiwei
 */
public class ComplexExpression {
    /**
     * 两个数相加，返回和的密文
     * @param ciperX
     * @param ciperY
     * @param encNum
     * @return
     */
    public double[] addition(double[] ciperX, double[] ciperY, int encNum){
        double[] result = new double[encNum];
        for(int i = 0; i < encNum; i++){
            result[i] = ciperX[i] + ciperY[i];
        }
        return result;
    }
    
    public double[] multiplication(double ciperX[], double[] ciperY, int encNum, double[][] keys){
        FHOPEAlgorithm fhopeAlg = new FHOPEAlgorithm();
        double[] result = new double[encNum];
        double[][] tempResult = new double[encNum][encNum];
        for(int i = 0; i < encNum; i++){
            for(int j= 0; j < encNum; j++){
                tempResult[i][j] = ciperX[i] * ciperY[j];
            }
            result[i] = fhopeAlg.decryptFunc(tempResult[i], keys, encNum);
        }
        return result;
    }
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        double a = 10.1;
        double b = 2.0;
        double c = 1.25;
        double d = 3.23;
        //明文下 a * b + c > d, 下面我们对a,b,c,d分别进行加密，用他们的密文进行比较
        FHOPEAlgorithm fhope = new FHOPEAlgorithm();
        int encNum = 5;
        double sens = 1E-8;
        double[][] keys = fhope.keyGenerate(encNum);
        double[][] noises = fhope.noiseGenerate(encNum, sens, keys);
        double[] ca = fhope.encryptFunc(a, keys, noises, encNum);
        double[] cb = fhope.encryptFunc(b, keys, noises, encNum);
        double[] cc = fhope.encryptFunc(c, keys, noises, encNum);
        double[] cd = fhope.encryptFunc(d, keys, noises, encNum);
        //下面进行多项式的运算
        ComplexExpression cp = new ComplexExpression();
        double[] leftExp = cp.addition(cp.multiplication(ca, cb, encNum, keys), cc, encNum);
        System.out.println(leftExp[0] + "---------" + cd[0]);

    }

}
